package formulasimulator;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class WorkingFormulaSimulator
{

    public static void main(String[] args)
    {
        
        try (ServerSocket serverSocket = new ServerSocket(2727))
        {
Socket clientSocket = null;
            
            while (true)
            {
                try
                {
                    System.out.println("** Waiting for client");
                    clientSocket = serverSocket.accept();
                    System.out.println("** Client connected");
                    clientSocket.setSoTimeout(5000);
                    clientSocket.setKeepAlive(true);
                    
                    try (PrintStream output = new PrintStream(clientSocket.getOutputStream()))
                    {
                        try (DataInputStream in = new DataInputStream(new FileInputStream("raw.bin")))
                        {
                        
                            byte[] buffer = new byte[2048];
                            int limit = 2340;
                            in.skipBytes(1760 * 2048);
                            while( limit-- > 0 )
                            {
                                in.read(buffer, 0, 2048);
                                output.print("OD Data");
                                output.write(10);
                                output.print("Data size: 2048");
                                output.write(10);
                                output.write(buffer);
                                try {
                                    Thread.sleep(99);
                                } catch (InterruptedException ex) {
                                    System.out.println("Thread interrupted: " + ex);
                                }
                            }
                        }
                        catch (EOFException ignored)
                        {
                            System.out.println("[EOF]");
                            output.close();
                        }
                        catch (FileNotFoundException ignored)
                        {
                            System.out.println("Input file not found");
                        }
                        catch (IOException ex)
                        {
                            System.out.println(ex);
                        }
                    }
                    catch (IOException ex)
                    {
                        System.out.println(ex);
                    }
                    clientSocket.close();
                    System.out.println("** Connection lost");
                }
                catch (IOException e)
                {
                    System.out.println(e);
                }
                
            }
        }
        catch (IOException ex)
        {
            System.out.println(ex);
        }
    }
}
